package com.bnlr.lrparking.appui.stall;

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bumptech.glide.Glide;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.StallM;
import com.bnlr.lrparking.view.CarCommon;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by hp on 2017/8/18.
 * 虚拟车位详情
 */

public class StallDetailsActivity extends BaseActivity {

    private Intent mintent;
    private TextView tvTitle;
    private TextView stalldetails_code,stalldetails_type,stalldetails_size,stalldetails_their,
            stalldetails_num,stalldetails_price_num,stalldetails_time,stalldetails_location;
    private RelativeLayout rely_details_goinvest,rely_details_phone,rely_details_collsge;
    private ImageView img_stalldetails;
    private CheckedTextView img_stalldetils_coll;
    private String IsFavorite="";
    private CarCommon carCommon=new CarCommon();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stalletails);
        initView();
    }

    private void initView(){
        mintent = getIntent();
        tvTitle=(TextView)findViewById(R.id.tv_nav_title);
        tvTitle.setText("利人停车详情");
        img_stalldetails=(ImageView)findViewById(R.id.img_stalldetails);
        img_stalldetils_coll=(CheckedTextView) findViewById(R.id.img_stalldetils_coll);
        stalldetails_code=(TextView)findViewById(R.id.stalldetails_code);
        stalldetails_type=(TextView)findViewById(R.id.stalldetails_type);

        stalldetails_size=(TextView)findViewById(R.id.stalldetails_size);
        stalldetails_their=(TextView)findViewById(R.id.stalldetails_their);
        stalldetails_num=(TextView)findViewById(R.id.stalldetails_num);
        stalldetails_price_num=(TextView)findViewById(R.id.stalldetails_price_num);
        stalldetails_time=(TextView)findViewById(R.id.stalldetails_time);
        stalldetails_location=(TextView)findViewById(R.id.stalldetails_location);
        stalldetails_code.setText("车位编号："+mintent.getStringExtra("number"));
        stalldetails_type.setText("车位类型："+mintent.getStringExtra("type"));
        stalldetails_size.setText("车位大小："+mintent.getStringExtra("size"));
        stalldetails_their.setText("车位所有者："+mintent.getStringExtra("their"));
        Glide.with(baseContext).load(mintent.getStringExtra("parkphoto")).placeholder(R.mipmap.tab2_defaultgraph1).error(R.mipmap.tab2_defaultgraph1).into(img_stalldetails);
        stalldetails_num.setText(mintent.getStringExtra("incomerockon"));
        stalldetails_price_num.setText(mintent.getStringExtra("sellingprice"));
        stalldetails_time.setText("可投资时间："+mintent.getStringExtra("time")+"-"+mintent.getStringExtra("time1"));
        stalldetails_location.setText(""+mintent.getStringExtra("address"));
        IsFavorite=mintent.getStringExtra("IsFavorite");
        rely_details_goinvest=(RelativeLayout)findViewById(R.id.rely_details_goinvest);
        rely_details_phone=(RelativeLayout)findViewById(R.id.rely_details_phone);
        rely_details_collsge=(RelativeLayout)findViewById(R.id.rely_details_collsge);
        rely_details_goinvest.setOnClickListener(this);
        rely_details_phone.setOnClickListener(this);
        rely_details_collsge.setOnClickListener(this);
        if (IsFavorite.equals("0")){
            img_stalldetils_coll.setChecked(false);
        }else if (IsFavorite.equals("1")){
            img_stalldetils_coll.setChecked(true);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.rely_details_goinvest:
                if (mintent.getStringExtra("monthCount").equals("0")) {
                    ToastUtils.showShort("该车位已被投资，请选择其他车位投资");
                } else{
                    Intent intent = new Intent(StallDetailsActivity.this, InvestActivity.class);
                    intent.putExtra("number", mintent.getStringExtra("number"));//
                    intent.putExtra("id", mintent.getStringExtra("id"));
                    intent.putExtra("sellingprice", mintent.getStringExtra("sellingprice"));
                    intent.putExtra("monthCount", mintent.getStringExtra("monthCount"));
                    startActivity(intent);
                }
                break;
            case R.id.rely_details_phone:
                carCommon.phoneDialog(this);
                break;
            case R.id.rely_details_collsge://收藏
                startShakeByPropertyAnim(rely_details_collsge, 0.9f, 1.2f, 11f, 500);
                if (IsFavorite.equals("0")) {
                    setCollege(mintent.getStringExtra("id"), "FavoriteSpace");
                }else if (IsFavorite.equals("1")){
                    setCollege(mintent.getStringExtra("id"), "CancelFavoriteSpace");
                }
                break;
        }
    }
    //是点击的动画
    private void startShakeByPropertyAnim(View view, float scaleSmall, float scaleLarge, float shakeDegrees, long duration) {
        if (view == null) {
            return;
        }
        //TODO 验证参数的有效性

        //先变小后变大
        PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                Keyframe.ofFloat(0f, 1.0f),
                Keyframe.ofFloat(0.25f, scaleSmall),
                Keyframe.ofFloat(0.5f, scaleLarge),
                Keyframe.ofFloat(0.75f, scaleLarge),
                Keyframe.ofFloat(1.0f, 1.0f)
        );
        PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                Keyframe.ofFloat(0f, 1.0f),
                Keyframe.ofFloat(0.25f, scaleSmall),
                Keyframe.ofFloat(0.5f, scaleLarge),
                Keyframe.ofFloat(0.75f, scaleLarge),
                Keyframe.ofFloat(1.0f, 1.0f)
        );

        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, scaleXValuesHolder, scaleYValuesHolder, scaleYValuesHolder);
        objectAnimator.setDuration(duration);
        objectAnimator.start();
    }


    //收藏的接口
    private void setCollege(String id, String FavoriteSpace){
        try {
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            /**
             * IsFavorite: 0是未收藏  1是已收藏
             * action: FavoriteSpace是从藏接口  CancelFavoriteSpace是取消收藏接口
             */
            mRequest.add("action", FavoriteSpace);
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("id", id);

            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<StallM>(baseContext, true, StallM.class) {
                        @Override
                        public void doWork(StallM data, String code) {
                            if ("1".equals(code)) {
                                if (IsFavorite.equals("0")){
                                    img_stalldetils_coll.setChecked(true);
                                    IsFavorite="1";

                                }else if(IsFavorite.equals("1")){
                                    img_stalldetils_coll.setChecked(false);
                                    IsFavorite="0";

                                }
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }
}
